home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / dejagnu.lha / dejagnu-1.0.1 / expect / exp_pty.c < prev    next >
C/C++ Source or Header  |  1993-04-26  |  5KB  |  237 lines

  1. /* exp_pty.c - generic routines to allocate and test ptys
  2.  
  3. Written by: Don Libes, NIST,  3/9/93
  4.  
  5. Design and implementation of this program was paid for by U.S. tax
  6. dollars.  Therefore it is public domain.  However, the author and NIST
  7. would appreciate credit if this program or parts of it are used.
  8.  
  9. */
  10.  
  11. #include "exp_conf.h"
  12. #ifdef HAVE_UNISTD_H
  13. #  include <unistd.h>
  14. #endif
  15. #ifdef HAVE_SYS_FCNTL_H
  16. #  include <sys/fcntl.h>
  17. #else
  18. #  include <fcntl.h>
  19. #endif
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <signal.h>
  23. #include <setjmp.h>
  24. #include <sys/file.h>
  25. #include "exp_rename.h"
  26. #include "exp_pty.h"
  27.  
  28. extern int errno;
  29.  
  30. void debuglog();
  31.  
  32. #ifndef TRUE
  33. #define TRUE 1
  34. #define FALSE 0
  35. #endif
  36.  
  37. #ifdef O_NOCTTY
  38. #define RDWR ((O_RDWR)|(O_NOCTTY))
  39. #else
  40. #define RDWR O_RDWR
  41. #endif
  42.  
  43. static int locked = FALSE;
  44. static char lock[] = "/tmp/ptylock.XX";    /* XX is replaced by pty id */
  45. static char locksrc[50] = "/tmp/expect.pid"; /* pid is replaced by real pid */
  46.     /* locksrc is used as the link source, i.e., something to link from */
  47.  
  48. static int i_read_errno;/* place to save errno, if i_read() == -1, so it
  49.                doesn't get overwritten before we get to read it */
  50. static jmp_buf env;        /* for interruptable read() */
  51. static int env_valid = FALSE;    /* whether we can longjmp or not */
  52.  
  53. /* sigalarm_handler and i_read are here just for supporting the sanity */
  54. /* checking of pty slave devices.  I have only seen this happen on BSD */
  55. /* systems, but it may need to be done to the other pty implementations */
  56. /* as well. */
  57.  
  58. /* Note that this code is virtually replicated from other code in expect */
  59. /* At some point, I'll dump one, but not until I'm satisfied no other */
  60. /* changes are needed */
  61.  
  62. /*ARGSUSED*/
  63. static RETSIGTYPE
  64. sigalarm_handler(n)
  65. int n;        /* unused, for compatibility with STDC */
  66. {
  67. #ifdef REARM_SIG
  68.     signal(SIGALRM,sigalarm_handler);
  69. #endif
  70.  
  71.     /* check env_valid first to protect us from the alarm occurring */
  72.     /* in the window between i_read and alarm(0) */
  73.     if (env_valid) longjmp(env,1);
  74. }
  75.  
  76. /* interruptable read */
  77. static int
  78. i_read(fd,buffer,length,timeout)
  79. int fd;
  80. char *buffer;
  81. int length;
  82. int timeout;
  83. {
  84.     int cc = -2;
  85.  
  86.     /* since setjmp insists on returning 1 upon longjmp(,0), */
  87.     /* longjmp(,2) instead. */
  88.  
  89.     /* restart read if setjmp returns 0 (first time) or 2. */
  90.     /* abort if setjmp returns 1. */
  91.  
  92.     alarm(timeout);
  93.  
  94.     if (1 != setjmp(env)) {
  95.         env_valid = TRUE;
  96.         cc = read(fd,buffer,length);
  97.     }
  98.     env_valid = FALSE;
  99.     i_read_errno = errno;    /* errno can be overwritten by the */
  100.                 /* time we return */
  101.     alarm(0);
  102.     return(cc);
  103. }
  104.  
  105. static RETSIGTYPE (*func)();    /* save old sigalarm handler */
  106. static time_t current_time;    /* time when testing began */
  107.  
  108. /* if TRUE, begin testing, else end testing */
  109. /* returns -1 for failure, 0 for success */
  110. int
  111. exp_pty_test_start()
  112. {
  113.     int lfd;    /* locksrc file descriptor */
  114.  
  115.     func = signal(SIGALRM,sigalarm_handler);
  116.  
  117.     time(¤t_time);
  118.  
  119.     /* recreate locksrc to prevent locks from 'looking old', so */
  120.     /* that they are not deleted (later on in this code) */
  121.     sprintf(locksrc,"/tmp/expect.%d",getpid());
  122.     (void) unlink(locksrc);
  123.     if (-1 == (lfd = creat(locksrc,0777))) {
  124.         debuglog("can't create %s, errno = %d\n",locksrc, errno);
  125.         return(-1);
  126.     }
  127.     close(lfd);
  128.     return 0;
  129. }
  130.  
  131. void
  132. exp_pty_test_end()
  133. {
  134.     signal(SIGALRM,func);
  135.     (void) unlink(locksrc);
  136. }
  137.  
  138. /* returns non-negative if successful */
  139. int
  140. exp_pty_test(master_name,slave_name,bank,num)
  141. char *master_name;
  142. char *slave_name;
  143. int bank;
  144. int num;
  145. {
  146.     int master, slave;
  147.     int cc;
  148.     char c;
  149.  
  150.     /* make a lock file to prevent others (for now only */
  151.     /* expects) from allocating pty while we are playing */
  152.     /* with it.  This allows us to rigorously test the */
  153.     /* pty is usable. */
  154.     if (exp_pty_lock(bank,num) == 0) {
  155.         debuglog("pty master (%s) is locked...skipping\r\n",master_name);
  156.         return(-1);
  157.     }
  158.     /* verify no one else is using slave by attempting */
  159.     /* to read eof from master side */
  160.     if (0 > (master = open(master_name,RDWR))) return(-1);
  161. #ifdef HAVE_PTYTRAP
  162.     if (access(slave_name, R_OK|W_OK) != 0) {
  163.         debuglog("could not open slave for pty master (%s)...skipping\r\n",
  164.             master_name);
  165.         (void) close(master);
  166.         return -1;
  167.     }
  168.     return(master);
  169. #else
  170.     if (0 > (slave = open(slave_name,RDWR))) {
  171.         (void) close(master);
  172.         return -1;
  173.     }
  174.     (void) close(slave);
  175.     cc = i_read(master,&c,1,10);
  176.     (void) close(master);
  177.     if (!(cc == 0 || cc == -1)) {
  178.         debuglog("%s slave open, skipping\r\n",slave_name);
  179.         return -1;
  180.     }
  181.  
  182.     /* verify no one else is using master by attempting */
  183.     /* to read eof from slave side */
  184.     if (0 > (master = open(master_name,RDWR))) return(-1);
  185.     if (0 > (slave = open(slave_name,RDWR))) {
  186.         (void) close(master);
  187.         return -1;
  188.     }
  189.     (void) close(master);
  190.     cc = i_read(slave,&c,1,10);
  191.     (void) close(slave);
  192.     if (!(cc == 0 || cc == -1)) {
  193.         debuglog("%s master open, skipping\r\n",master_name);
  194.         return -1;
  195.     }
  196.  
  197.     /* seems ok, let's use it */
  198.     return(open(master_name,RDWR));
  199. #endif
  200. }
  201.  
  202. void
  203. exp_pty_unlock()
  204. {
  205.     if (locked) {
  206.     (void) unlink(lock);
  207.     locked = FALSE;
  208. }
  209. }
  210.  
  211. /* returns 1 if successfully locked, 0 otherwise */
  212. int
  213. exp_pty_lock(bank,num)
  214. int bank;
  215. int num;
  216. {
  217.     struct stat statbuf;
  218.  
  219.     if (locked) {
  220.         unlink(lock);
  221.         locked = FALSE;
  222.     }
  223.  
  224.     sprintf(lock,"/tmp/ptylock.%c%c",bank,num);
  225.  
  226.     if ((0 == stat(lock,&statbuf)) &&
  227.         (statbuf.st_mtime+3600 < current_time)) {
  228.         (void) unlink(lock);
  229.     }
  230.  
  231.     if (-1 == (link(locksrc,lock))) locked = FALSE;
  232.     else locked = TRUE;
  233.  
  234.     return locked;
  235. }
  236.  
  237.